package com.inspur.gs.sbssdl.gzgl.ssqyj.ssqyj.back.bizactions;
import com.inspur.edp.bef.api.lcp.IStandardLcp;
import com.inspur.edp.bef.core.LcpUtil;
import com.inspur.edp.cef.entity.changeset.*;
import com.inspur.edp.bef.api.be.*;
import com.inspur.edp.bef.api.action.VoidActionResult;
import com.inspur.edp.bef.spi.action.AbstractManagerAction;
import com.inspur.edp.cef.entity.entity.IEntityData;
import com.inspur.edp.cef.entity.entity.IEntityDataCollection;
import com.inspur.edp.cef.entity.entity.IValuesContainer;

import java.util.ArrayList;

public class gzssqyjunLockMgrAction extends AbstractManagerAction<VoidActionResult> {
	private String id;
	public gzssqyjunLockMgrAction(IBEManagerContext managerContext, String id) {
		super(managerContext);
		this.id = id;
	}
	// 所属权
	private String ssqyjCfg = "com.inspur.gs.sbssdl.gzgl.ssqyj.ssqyj.back.gzssqyj";

	// 工装台账
	private String gztzCfg = "com.inspur.gs.sbssdl.gzgl.ledger.ledger.back.FrockLedger";

	/**
	 * 退还锁定
	 * 1.扣除锁定数量
	 * 2.删除提交日志
	 */
	@Override
	public  void execute() {
		IStandardLcp ssqLcp = LcpUtil.getLcp(ssqyjCfg);
		IStandardLcp gztzLcp = LcpUtil.getLcp(gztzCfg);

		IEntityData ssqData = ssqLcp.retrieve(this.id).getData();
		IEntityDataCollection ssqMx = ssqData.getChilds().get("yjmx");

		for (IEntityData mx : ssqMx) {
			unlockKc(ssqData,mx,gztzLcp);
		}
	}
	private IBusinessEntity getEntity(String dataId) {
		return (IBusinessEntity)super.getBEManagerContext().getEntity(dataId);
	}	private IBEService getMgr() {
		return (IBEService)super.getBEManagerContext().getBEManager();
	}

	private void unlockKc(IEntityData ssqData, IEntityData mx, IStandardLcp gzLcp) {
		String mxId = mx.getID();

		IValuesContainer gztz = (IValuesContainer) mx.getValue("gzid");
		if (gztz == null || gztz.getValue("gzid") == null) {
			return;
		}
		String gzid = gztz.getValue("gzid").toString();
		IEntityData gzData = gzLcp.retrieve(gzid).getData();

		int zmsl = Integer.parseInt(gzData.getValue("zmsl").toString());
		int sdsl = Integer.parseInt(gzData.getValue("sdsl").toString());
		int mxSl = Integer.parseInt(mx.getValue("sl").toString());

		// 检查扣减日志是否存在,不存在则返回
		IEntityDataCollection yjjls = gzData.getChilds().get("yjjl");
		boolean hasItem = yjjls.stream().anyMatch(item -> item.getValue("yjmxId") != null && item.getValue("yjmxId").toString().equals(mxId));
		if (!hasItem) return;

		// 解锁数量
		ModifyChangeDetail kcMcd = new ModifyChangeDetail(gzid);
		kcMcd.putItem("sdsl", sdsl - mxSl);

		// 删除日志
		ArrayList<String> nodeCodes = new ArrayList<>();
		nodeCodes.add("yjjl");
		ArrayList<String> hierarchyIds = new ArrayList<>();
		hierarchyIds.add(gzid);

		ArrayList<String> delIds = new ArrayList<>();
		for (IEntityData yjjl : yjjls) {
			if (yjjl.getValue("yjmxId") != null){
				String yjmxId = yjjl.getValue("yjmxId").toString();
				if (yjmxId.equals(mxId)) {
					delIds.add(yjjl.getID());
				}
			}
		}

		// 提交
		gzLcp.deleteChild(nodeCodes,hierarchyIds,delIds);
		gzLcp.modify(kcMcd);
		gzLcp.save();
	}
}